<template>
  <div>
    <div v-if="editVisible">
      <div class="form-panel">
        <!-- 搜索表单 -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="试题题目" prop="questionsTitle">
            <el-input
              v-model="queryParams.questionsTitle"
              placeholder="请输入试题题目"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="试题类型" prop="questionsType">
            <el-select v-model="queryParams.questionsType" placeholder="请选择试题类型" clearable size="small">
              <el-option
                v-for="dict in questionsTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 工具栏 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['questions:examquestions:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['questions:examquestions:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
              v-hasPermi="['questions:examquestions:export']"
            >导出</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['questions:examquestions:import']"
            >导入</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 主体内容 -->
        <el-row :gutter="10">
          <!-- 左侧分类树 -->
          <el-col :span="3">
            <div class="head-container">
              <el-input
                v-model="typeName"
                placeholder="请输入类型名称"
                clearable
                size="small"
                prefix-icon="el-icon-search"
                style="margin-bottom: 20px"
              />
            </div>
            <div class="head-container">
              <el-tree
                :data="examtypeList"
                :props="defaultProps"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                ref="tree"
                check-strictly
                default-expand-all
                @node-click="handleNodeClick"
              />
            </div>
          </el-col>
          <!-- 右侧列表数据 -->
          <el-col :span="21">
            <el-table v-loading="loading" :data="examquestionsList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="序号" type="index" width="50" align="center">
                <template slot-scope="scope">
                  <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="试题题目" align="center" prop="questionsTitle" />
              <el-table-column label="试题状态" width="80" align="center" prop="status"  >
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.status==0  " size="small" type="warning">停用
                  </el-tag>
                  <el-tag v-if="scope.row.status==1 " size="small" type="primary">启用
                  </el-tag>
                </template>

              </el-table-column>
              <el-table-column label="试题类型" width="80" align="center" prop="questionsType" :formatter="questionsTypeFormat" />
<!--              <el-table-column label="分数" width="60" align="center" prop="questionsScore" />-->
<!--              <el-table-column label="复杂度" width="150" align="center">-->
<!--                <template slot-scope="scope">-->
<!--                  <el-rate v-model="scope.row.rateNumber" disabled text-color="#ff9900" />-->
<!--                </template>-->
<!--              </el-table-column>-->
              <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['questions:examquestions:edit']"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['questions:examquestions:remove']"
                  >删除</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-turn-off"
                    @click="handleStatus(scope.row)"
                    v-hasPermi="['questions:examquestions:edit']"
                  >{{ scope.row.status == '1' ? '禁用' : '启用' }}</el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="total > 0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </el-col>
        </el-row>
      </div>
    </div>
    <div v-else>
      <exam-questions ref="editRef" @refreshData="editDoneHandle"></exam-questions>
    </div>
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
      v-loading="upload.isUploading"
      element-loading-text="上传中..."
      element-loading-background="rgba(0, 0, 0, 0.7)"
      @before-close="handleDialogClose"
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport+'&bankCode='+form.bankCode"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :before-upload="handleBeforeUpload"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的试题数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-select clearable  style="width: 350px;"  v-model="form.bankCode" filterable placeholder="请选择题库" >
            <el-option
              v-for="bank in banks"
              :key="bank.bankId"
              :label="bank.bankName"
              :value="bank.bankCode">
            </el-option>
          </el-select>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>


<script>
import { listQuestionsproperty, getQuestionsproperty, delQuestionsproperty, addQuestionsproperty, updateQuestionsproperty, exportQuestionsproperty } from "@/api/questions/questionsproperty";
import {
  listExamquestions,
  getExamquestions,
  delExamquestions,
  addExamquestions,
  updateExamquestions,
  exportExamquestions,
  statusExamquestions
} from '@/api/questions/examquestions'
import ExamQuestions from './examQuestions'
import { getToken } from '@/utils/auth'
import { baseApiUrl } from '@/config/sysinfo'
import { importTemplate } from '@/api/system/user'
import { typeTreeSelect } from '@/api/questions/examtype'
import { listQuestionsbankNoPage } from '@/api/questions/questionsbank'

export default {
  name: "Questionslist",
  components: {
    ExamQuestions
  },
  data() {
    return {
      banks:[],
      typeName:'',
      examtypeList: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/questions/questionsproperty/importData"
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      bankData: null,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      editVisible: true,
      // 考试题目表格数据
      examquestionsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 试题id字典
      questionsIdOptions: [],
      // 试题代码字典
      questionsCodeOptions: [],
      // 试题题目字典
      questionsTitleOptions: [],
      // 试题内容字典
      questionsContentOptions: [],
      // 试题类型字典
      questionsTypeOptions: [
        {dictValue: 1, dictLabel: '判断题'},
        {dictValue: 2, dictLabel: '单选题'},
        {dictValue: 3, dictLabel: '多选题'}
      ],
      // 题目分数字典
      questionsScoreOptions: [],
      // 复杂度字典
      rateNumberOptions: [],
      // 正确答案字典
      rightAnswerOptions: [],
      // 答案分析字典
      answerAnalyseOptions: [],
      // 状态字典
      statusOptions: [],
      // 创建者字典
      createByOptions: [],
      // 创建部门字典
      createDeptOptions: [],
      // 创建时间字典
      createTimeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bankCode: null,
        questionsTitle: null,
        questionsType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        questionsCode: [
          { required: true, message: "试题代码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
this.fetchBanks()
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.queryParams.pageNum = 1;
    this.getList();
  },
  watch: {
    // 根据名称筛选部门树
    typeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    fetchBanks(){
      listQuestionsbankNoPage({'source':1}).then(response => {
        this.banks = response.rows;
      });
    },
    handleNodeClick(data) {
      console.log('data:' + JSON.stringify(data))
      // this.queryParams.examType = data.id;
      let parentId = data.parentId
      if (parentId+''&&parentId+''=='0'){
        this.queryParams.examType = data.id;
        this.queryParams.bankCode = null;
      }else {
        this.queryParams.examType = null;
        this.queryParams.bankCode = data.bankCode;
      }

      // console.log('this.queryParams:' + JSON.stringify(this.queryParams))
      this.getList();
    },

    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    getTypeTreeselect() {
      typeTreeSelect().then(response => {
        this.examtypeList = response.data;
        // console.log('examtypeList:' + JSON.stringify(this.examtypeList))
      });
    },
    navigateToPage(row) {
      // 假设目标路由名为 'detailPage'，并传递行数据的某个标识符进行跳转
      this.$router.push({
        path: `/disk/file/`
      });
    },
    /** 查询考试题目列表 */
    init_data (row) {
      this.bankData = row
      if(this.bankData.bankCode){
        this.queryParams.bankCode = this.bankData.bankCode
      }

      this.getList()
    },
    getList() {
      this.loading = true;
      listQuestionsproperty(this.queryParams).then(response => {
        this.examquestionsList = response.rows;
        // console.log('this.examquestionsList:' + JSON.stringify(this.examquestionsList))
        this.total = response.total;
        this.loading = false;
        this.getTypeTreeselect()
      });
    },
    // 试题id字典翻译
    questionsIdFormat(row, column) {
      return this.selectDictLabel(this.questionsIdOptions, row.questionsId);
    },
    // 试题代码字典翻译
    questionsCodeFormat(row, column) {
      return this.selectDictLabel(this.questionsCodeOptions, row.questionsCode);
    },
    // 试题题目字典翻译
    questionsTitleFormat(row, column) {
      return this.selectDictLabel(this.questionsTitleOptions, row.questionsTitle);
    },
    // 试题内容字典翻译
    questionsContentFormat(row, column) {
      return this.selectDictLabel(this.questionsContentOptions, row.questionsContent);
    },
    // 试题类型字典翻译
    questionsTypeFormat(row, column) {
      return this.selectDictLabel(this.questionsTypeOptions, row.questionsType);
    },
    // 题目分数字典翻译
    questionsScoreFormat(row, column) {
      return this.selectDictLabel(this.questionsScoreOptions, row.questionsScore);
    },
    // 复杂度字典翻译
    rateNumberFormat(row, column) {
      return this.selectDictLabel(this.rateNumberOptions, row.rateNumber);
    },
    // 正确答案字典翻译
    rightAnswerFormat(row, column) {
      return this.selectDictLabel(this.rightAnswerOptions, row.rightAnswer);
    },
    // 答案分析字典翻译
    answerAnalyseFormat(row, column) {
      return this.selectDictLabel(this.answerAnalyseOptions, row.answerAnalyse);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      let status = row.status =='1'?'0':'1'
      return this.selectDictLabel(this.statusOptions, status);
    },
    // 创建者字典翻译
    createByFormat(row, column) {
      return this.selectDictLabel(this.createByOptions, row.createBy);
    },
    // 创建部门字典翻译
    createDeptFormat(row, column) {
      return this.selectDictLabel(this.createDeptOptions, row.createDept);
    },
    // 创建时间字典翻译
    createTimeFormat(row, column) {
      return this.selectDictLabel(this.createTimeOptions, row.createTime);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        questionsId: null,
        questionsCode: null,
        questionsTitle: null,
        questionsContent: null,
        questionsType: null,
        questionsScore: null,
        rateNumber: null,
        rightAnswer: null,
        answerAnalyse: null,
        status: 0,
        createBy: null,
        createDept: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.examType=null
      this.queryParams.bankCode=null
      this.handleQuery();
    },
    handleDialogClose() {
      // 清空 bankCode 的值
      this.form.bankCode = '';
      // 关闭 dialog
      this.upload.open = false;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.questionsCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    editDoneHandle () {
      this.editVisible = true
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editVisible = false
      this.$nextTick(() => {
        if (!this.bankData){
          this.bankData={}
          this.bankData.bankCode=null
        }
        this.$refs.editRef.init_data(this.bankData, null)
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editVisible = false
      this.$nextTick(() => {

        this.$refs.editRef.init_data(this.bankData, row)
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.questionsCode != null) {
            updateExamquestions(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExamquestions(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleImport() {

      this.upload.title = "试题导入";
      this.upload.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const questionsCodes = row.questionsCode || this.ids;
      this.$confirm('是否确认删除考试题目编号为"' + questionsCodes + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delExamquestions(questionsCodes);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleStatus(row) {
      const action = row.status === '1' ? '禁用' : '启用';
      const questionsCodes = row.questionsCode || this.ids;
      this.$confirm(`是否确认${action}考试题目编号为"${questionsCodes}"的数据项?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return statusExamquestions(questionsCodes);
      }).then(() => {
        this.msgSuccess('操作成功');
        this.getList();

      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有考试题目数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        // return exportExamquestions(queryParams);
        this.download('/questions/examquestions/export',{
          ...this.queryParams
        },`试题_${new Date().getTime()}.xlsx`)
      }).then(response => {
        // this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    onClose () {
      this.$emit('refreshDataList')
    },
    /** 下载模板操作 */
    importTemplate() {

      // this.download('/questions/questionsproperty/importTemplate');
      this.download('/questions/questionsproperty/importTemplate', {
      }, `试题模板_${new Date().getTime()}.xlsx`)
      // importTemplate().then(response => {
      //   this.download(response.msg);
      // });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 上传前校检格式和大小
    handleBeforeUpload(file) {
      if (!this.form.bankCode || this.form.bankCode.trim() === '') {
        this.$message.error('请选择题库后上传！');
        return false; // 阻止上传
      }else {
        return true; // 允许上传
      }},

    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();

    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>


<style scoped lang="scss">
.form-panel {
  margin:  10px 0px 20px 15px;
}
</style>

